package Hangman;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;
/**
 * Menu class, used for the menu system, where the used comes to first.
 * From this class the user will be able to enter settings and HangmanGame, as well
 * as exit the game completely.
 * @author Robin E, Jakob L, Isac A, Cristian O, Eric P, Tobias W. 
 *
 */

@SuppressWarnings("serial")
public class Menu extends JPanel{
	
	private JLabel headLineLabel;
	private JLabel startGameLabel;
	private JLabel settingsLabel;
	private JLabel exitGameLabel;
	
	private Settings settings = new Settings();
	private HangmanGame game = new HangmanGame();
	//start sound with menu song
	private Sound sound = new Sound("hangman-sounds/skyrim.mp3");
	
	private JPanel menuPanel;
	private JPanel cardlayoutPanel;
	private JPanel imagePanel;
	private JPanel contentPanel;
	
	private CardLayout cardLayout;
	
	private BufferedImage hangman;
	
	/**
	 * Menu Contructor, initializes all components in the class.
	 * Also adds all components to their respective JPanel.
	 */
	Menu()
	{
		setLayout(new BorderLayout());
		cardLayout = new CardLayout();
		cardlayoutPanel = new JPanel();
		cardlayoutPanel.setLayout(cardLayout);
		
		Font largeSizedFont = new Font("Arial", Font.BOLD, 40);
		Font mediumSizedFont = new Font("Arial", Font.BOLD, 20);
		
		headLineLabel = new JLabel("HANGMAN");
		headLineLabel.setFont(largeSizedFont);
		
		startGameLabel = new JLabel("Start Game");
		startGameLabel.addMouseListener(new startListener());
		startGameLabel.setFont(mediumSizedFont);
		
		settingsLabel = new JLabel("Game Settings");
		settingsLabel.addMouseListener(new settingsListener());
		settingsLabel.setFont(mediumSizedFont);
		
		exitGameLabel = new JLabel("Exit Game");
		exitGameLabel.addMouseListener(new exitListener());
		exitGameLabel.setFont(mediumSizedFont);
		
		menuPanel = new JPanel();
		menuPanel.setLayout(new MigLayout());
		menuPanel.setBackground(Color.white);
		
		imagePanel = new JPanel();
		imagePanel.setLayout(new MigLayout());
		imagePanel.setBackground(Color.white);
		
		contentPanel = new JPanel();
		contentPanel.setBackground(Color.white);
		contentPanel.setLayout(new MigLayout());
		
		try
		{
			hangman = ImageIO.read(new File("Images\\Hangman.jpg"));
		}catch(IOException e)
		{
			System.out.println("Cant find image");
		}
		
		JLabel tmp = new JLabel();
		tmp.setIcon(new ImageIcon(hangman));
		imagePanel.add(new JLabel(""), "wrap 50");
		imagePanel.add(tmp, "wrap 20");
		
		menuPanel.add(new JLabel(""), "wrap 160");
		menuPanel.add(headLineLabel);
		menuPanel.add(new JLabel(""), "wrap 20");
		menuPanel.add(startGameLabel, "gapbefore 50");
		menuPanel.add(new JLabel(""), "wrap");
		
		menuPanel.add(settingsLabel, "gapbefore 35");
		menuPanel.add(new JLabel(""), "wrap");
		
		menuPanel.add(exitGameLabel, "gapbefore 55");
		menuPanel.add(new JLabel(""), "wrap");
		
		contentPanel.add(imagePanel);
		contentPanel.add(menuPanel);
		
		cardlayoutPanel.add(contentPanel, "1");
		cardlayoutPanel.add(settings, "2");
		cardlayoutPanel.add(game, "3");
	
	}
	
	/**
	 * init() initialize the panels by showing them, adding them to the center of the screen
	 * and showing the current Panel (This)
	 */
	public void init()
	{
		add(cardlayoutPanel, BorderLayout.CENTER);
				
		cardLayout.show(cardlayoutPanel, "1");
		settings.hideWindow();
	}
	/**
	 * hideWindow() is a simple method to hide this JPanel
	 */
	public void hideWindow()
	{
		setVisible(false);
	}
	/**
	 * showWindow() is a simple method to show the JPanel and set the size of the JPanel.
	 */
	public void showWindow()
	{
		setSize(1000, 800);
		setVisible(true);
	}
	
	/**
	 * startListener is an inne class listener which handles 2 states of mouse cursors
	 * as well as changing which panel is shown which in this case is hangmanGame.
	 * @author Robin E, Jakob L, Isac A, Cristian O, Eric P, Tobias W. 
	 *
	 */
	public class startListener extends MouseAdapter
	{
		public void mouseEntered(MouseEvent mentered)
		{
			Cursor c = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
			setCursor(c);
		}
		public void mouseExited(MouseEvent mexit)
		{
			Cursor c = Cursor.getDefaultCursor();
			setCursor(c);
		}
		public void mouseReleased(MouseEvent mreleased)
		{
			sound.beepPool("hangman-sounds/beep.wav");
			cardLayout.show(cardlayoutPanel, "3");
			game.init();
			
		}
	}
	/**
	 * settingsListener is an inne class listener which handles 2 states of mouse cursors
	 * as well as changing which panel is shown which in this case is settings.
	 * @author Robin E, Jakob L, Isac A, Cristian O, Eric P, Tobias W. 
	 *
	 */
	public class settingsListener extends MouseAdapter
	{
		public void mouseEntered(MouseEvent mentered)
		{
			Cursor c = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
			setCursor(c);
		}
		public void mouseExited(MouseEvent mexit)
		{
			Cursor c = Cursor.getDefaultCursor();
			setCursor(c);
		}
		public void mouseReleased(MouseEvent mreleased)
		{
			sound.beepPool("hangman-sounds/beep.wav");
			cardLayout.show(cardlayoutPanel, "2");
			settings.init();
			
		}
	}
	/**
	 * exitListener is an inne class listener which handles 2 states of mouse cursors
	 * as well as exiting the game completly
	 * @author Robin E, Jakob L, Isac A, Cristian O, Eric P, Tobias W. 
	 *
	 */
	public class exitListener extends MouseAdapter
	{
		public void mouseEntered(MouseEvent mentered)
		{
			Cursor c = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
			setCursor(c);
		}
		public void mouseExited(MouseEvent mexit)
		{
			Cursor c = Cursor.getDefaultCursor();
			setCursor(c);
		}
		public void mouseReleased(MouseEvent mreleased)
		{
			sound.beepPool("hangman-sounds/tiger.wav");
			int choice = JOptionPane.showConfirmDialog(null, "Are you sure you wish to exit Hangman?", 
					"Warning", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
			
			if(choice == JOptionPane.YES_OPTION)
			{
				hideWindow();
				System.exit(0);
			}			
		}
	}


}
